package com.wind.adapter;

import android.annotation.SuppressLint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.wind.beans.ArticleListBean;
import com.debug.soblogin.R;
import com.wind.soblogin.DebugApplication;

import java.util.List;

public class ArticleListAdapter extends RecyclerView.Adapter<ArticleListAdapter.InnerHolder>{

    private final List<ArticleListBean.DataDTO.ContentsDTO> mData;
    private OnItemClickListener mOnItemClickListener;
    public ArticleListAdapter(List<ArticleListBean.DataDTO.ContentsDTO> mData) {
        this.mData = mData;
    }

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_liner_articles_content, null);
        return new InnerHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        holder.setData(mData.get(position), position);
    }

    @Override
    public int getItemCount() {
        if (mData != null) {
            return mData.size();
        }
        return 0;
    }
    public  void setOnItemClickListener(OnItemClickListener listener){
        //设置一个监听，就是设置一个接口，一个回调接口
        this.mOnItemClickListener = listener;

    }
    public interface OnItemClickListener{
        void onItemClick(int position);
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        private final TextView mTitle;
        private final TextView mSummary;
        private final TextView mTime;
        private final TextView mViewCount;
        private final ImageView mCover;
        private int mPosition;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            mTitle = itemView.findViewById(R.id.articles_title);
            mSummary = itemView.findViewById(R.id.articles_summary);
            mTime = itemView.findViewById(R.id.articles_time);
           mViewCount = itemView.findViewById(R.id.articles_view_count);
            mCover = itemView.findViewById(R.id.articles_cover);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(mPosition);
                    }
                }
            });
        }

        @SuppressLint("SetTextI18n")
        public void setData(ArticleListBean.DataDTO.ContentsDTO contentsDTO, int position) {
            this.mPosition = position;
            mTitle.setText(contentsDTO.getTitle());
            mSummary.setText(contentsDTO.getSummary());
            //todo:这里的时间要根据发表日期展示不同的类型。如三天之内显示今天，昨天，前天。时间更远的显示日期
            mTime.setText(contentsDTO.getCreateTime().substring(5, 10)+"  "+contentsDTO.getCreateTime().substring(11, 16));
            mViewCount.setText("被"+contentsDTO.getViewCount()+"人看爆了");
            //Glide.with(getContext()).load(contentsDTO.getCover()).into(mCover);
            Glide.with(DebugApplication.getContext()).load(contentsDTO.getCover()).apply(RequestOptions.bitmapTransform(new RoundedCorners(20))).into(mCover);
        }
    }
}
